package myself.draw.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import myself.draw.service.DrawService;
import myself.entity.ExamQuestionEntity;
import myself.login.service.LoginService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class DrawController {

	Logger logger = LoggerFactory.getLogger(DrawController.class);

	@Resource
	private LoginService loginService;

	@Resource
	private DrawService drawService;

	@RequestMapping(value = "/drawList.do")
	private ModelAndView draw(HttpServletRequest request,
			@RequestParam("page") Integer page) {

		HttpSession session = request.getSession();

		String userCode = (String) session.getAttribute("userCode");

		String userName = loginService.qryteacherName(userCode);

		request.setAttribute("userName", userName);

		request.setAttribute("topic", "all");
		
		String topic = "0";

		int sum = drawService.qryCount(userCode, topic);
		if (page == 0) {
			page = 1;
		}
		
		request.setAttribute("page", page);
		
		// 返回总页数
		request.setAttribute("sumCount", sum);

		// 分页每页5条
		int everyPageCount = 5;

		page = (page - 1) * everyPageCount;

		int sumPage = sum % everyPageCount == 0 ? sum / everyPageCount : sum
				/ everyPageCount + 1;

		request.setAttribute("sumPage", sumPage);

		List<ExamQuestionEntity> examQuestionEntitys = drawService
				.qryAllQuestins(userCode, page, everyPageCount,topic);
		// 返回 当前页的数据
		request.setAttribute("examQuestionEntitys", examQuestionEntitys);

		
		System.out.println("ss::::" + examQuestionEntitys);
		return new ModelAndView("WEB-INF/jsp/draw/drawList.jsp");
	}

	// 筛选选择题还是填空题的接口

	@RequestMapping(value = "/drawListByTopic.do")
	public ModelAndView drawListByTopic(HttpServletRequest request,
			@RequestParam("topic") String topic,
			@RequestParam("page") Integer page) {

		HttpSession session = request.getSession();

		String userCode = (String) session.getAttribute("userCode");

		String userName = loginService.qryteacherName(userCode);

		request.setAttribute("topic", topic);
		
		request.setAttribute("userName", userName);

		if (topic.equals("all")) {
			topic = "0";
		} else if (topic.equals("choose")) {
			topic = "1";
		} else {
			topic = "2";
		}

		int sum = drawService.qryCount(userCode, topic);

		if (page == 0) {
			page = 1;
		}

		request.setAttribute("page", page);
		
		// 返回总页数
		request.setAttribute("sumCount", sum);

		// 分页每页5条
		int everyPageCount = 5;

		page = (page - 1) * everyPageCount;

		int sumPage = sum % everyPageCount == 0 ? sum / everyPageCount : sum
				/ everyPageCount + 1;

		request.setAttribute("sumPage", sumPage);

		
		List<ExamQuestionEntity> examQuestionEntitys = drawService
				.qryAllQuestins(userCode, page, everyPageCount,topic);
		// 返回 当前页的数据
		request.setAttribute("examQuestionEntitys", examQuestionEntitys);
		
		return new ModelAndView("WEB-INF/jsp/draw/drawList.jsp");
	}
	
	//删除 题目---- 软删除
	
	@RequestMapping(value = "/deleteById.do")
	public String deleteById(HttpServletRequest request,
			@RequestParam("id") Integer id){
		
		
		int i = drawService.delById(id);
		
		if(i==0){
			return "0";
		}
		
		return "1";
	}
	

}
